<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=GzYjrkaTLoCd9xiMIbEkrb9HDCyuUmnN"></script>
	<title>单个标注点沿直线的轨迹运动</title>
</head>
<body>
	<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
	// 百度地图API功能
	var map = new BMap.Map("allmap");
	var point = null;
	var myP1 = null;
	var myP2 = null;
	// 创建地址解析器实例
	var myGeo = new BMap.Geocoder();
	// 将地址解析结果显示在地图上,并调整地图视野
	myGeo.getPoint("济南市历下区花园路泉景恒展商务大厦", function(point){
		if (point) {
			myP1 = new BMap.Point(point.lng,point.lat);    // 起点1
		}else{
			alert("您选择地址没有解析到结果!");
		}
	}, "济南市");
	var point = myP1;
	map.centerAndZoom(point,5);
	myGeo.getPoint("甘肃省天水市秦州区兰天城市广场", function(point){
		if (point) {
			myP2 = new BMap.Point(point.lng,point.lat);    // 起点1
		}else{
			alert("您选择地址没有解析到结果!");
		}
	}, "天水市");
	
	var myIcon = new BMap.Icon("5fafc99a.gif", new BMap.Size(100, 200), {    //小车图片
		//offset: new BMap.Size(0, -5),    //相当于CSS精灵
		imageOffset: new BMap.Size(0, 0)    //图片的偏移量。为了是图片底部中心对准坐标点。
	  });
	var transit = new BMap.DrivingRoute(map, {
		renderOptions: {
			map: map,
			panel: "r-result",
		},  
	});
	transit.search("myP1","myP2");   //显示一条公交线路

	window.run = function (){
		var driving = new BMap.DrivingRoute(map);    //驾车实例
		driving.search(myP1, myP2);
		driving.setSearchCompleteCallback(function(){
			var pts = driving.getResults().getPlan(0).getRoute(0).getPath();    //通过驾车实例，获得一系列点的数组
			var paths = pts.length;    //获得有几个点

			var carMk = new BMap.Marker(pts[0],{icon:myIcon});
			map.addOverlay(carMk);
			i=0;
			function resetMkPoint(i){
				carMk.setPosition(pts[i]);
				if(i < paths){
					setTimeout(function(){
						i++;
						resetMkPoint(i);
					},100);
				}
			}
			setTimeout(function(){
				resetMkPoint(5);
			},100)

		});
	}

	setTimeout(function(){
		run();
	},1500);
</script>
